<?php

	namespace Phptricks\system\library\database\database_helper;

	/**
	 * phptricks Framework 
	 * link http://phptricks.net/Framework
	 * date start 2014
	 * Description of database_helper
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\system\core\library\library;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\library\database\database as DataBase;

	class database_helper implements library{

		private static $_instance = null;
		private $db;

		public function __construct(){
			$this->db = DataBase::getinstance();
		}

		public static function getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\database\database_helper\database_helper();
			}
			return self::$_instance;
		}

		public function _initialize(){
			
		}

		public function Deletefield($table_name, $array_fields = array()){
			try {
				if (empty($table_name)) {
					throw new Exc('$table_name is not set');
				}
				if (empty($array_fields)) {
					throw new Exc('$field_nameis not set');
				}
				$string = '';
				if (is_array($array_fields)) {
					$string = 'DROP `' . \implode('`, DROP `', $array_fields) . '`';
				} else {
					$string = 'DROP `' . $array_fields . '`';
				}
				$alter = $this->db->prepare("ALTER TABLE `" . $table_name . "` " . $string . ";");
				$alter->execute();
				$error = $alter->errorInfo();
				if (isset($error[2]) && !empty($error[2])) {
					throw new Exc($error[2] . " : \n <strong> SQL:</strong>\t  \"" . 'ALTER TABLE `' . $table_name . '` ' . $array_fields . ';"');
					throw new Exc($error[2]);
				}
			} catch (\PDOException $exc) {
				throw new Exc($exc->getTraceAsString());
			}
			return true;
		}

		public function Addfield($table_name, $array_fields = array()){
			try {
				if (empty($table_name)) {
					throw new Exc('$table_name is not set');
				}
				if (!isset($array_fields)) {
					throw new Exc('$array_fields not set');
				}
				$string = '';
				if (is_array($array_fields)) {
					$string = 'DROP `' . \implode('`, DROP `', $array_fields) . '`';
				} else {
					$string = 'DROP `' . $array_fields . '`';
				}

				$alter = $this->db->prepare("ALTER TABLE `" . $table_name . "` " . $string . ";");
				$alter->execute();
				$error = $alter->errorInfo();
				if (isset($error[2]) && !empty($error[2])) {
					throw new Exc($error[2] . " : \n <strong> SQL:</strong>\t  \"" . 'ALTER TABLE `' . $table_name . '` ' . $string . ';"');
				}
			} catch (\PDOException $exc) {
				throw new Exc($exc->getTraceAsString());
			}
			return true;
		}

		public function DeleteTable($table_name){
			try {
				if (empty($table_name)) {
					throw new Exc('$table_name is not set');
				}
				$drop = $this->db->prepare("DROP TABLE `" . $table_name . "`");
				$drop->execute();
				$error = $drop->errorInfo();
				if (isset($error[2]) && !empty($error[2])) {
					throw new Exc($error[2]);
				}
			} catch (\PDOException $exc) {
				throw new Exc($exc->getTraceAsString());
			}
			return true;
		}

		public function EmptyTable($table_name){
			try {
				if (empty($table_name)) {
					throw new Exc('$table_name is not set');
				}
				$truncate = $this->db->prepare("TRUNCATE `" . $table_name . "`");
				$truncate->execute();
				$error = $truncate->errorInfo();
				if (isset($error[2]) && !empty($error[2])) {
					throw new Exc($error[2]);
				}
			} catch (\PDOException $exc) {
				throw new Exc($exc->getTraceAsString());
			}
			return true;
		}

	}
	